package atm;

import Kechang.SuperArray;

import javax.swing.*;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

/**
 * 转账界面
 */
public class TransferPanel extends JPanel implements ActionListener {
    private ATMFrame atmFrame;
    private ImageIcon Transfer= new ImageIcon("转账");
    private JLabel accountLabel = new JLabel("转账账号：");
    private JLabel accountLabel1 = new JLabel("转账余额：");
    private JTextField accountField = new JTextField();
    private JTextField accountField1= new JTextField();

    private JButton loginBtn = new JButton("转账");
    private Font font = new Font("黑体",Font.BOLD,30);



    public TransferPanel(ATMFrame atmFrame){
        this.atmFrame =atmFrame;
        this.setLayout(null);
        this.add(accountLabel);
        accountLabel.setBounds(250,200,200,50);
        accountLabel.setFont(font);
        accountLabel1.setBounds(250,260,200,50);
        accountLabel1.setFont(font);

        accountField.setBounds(400,200,250,50);
        accountField.setFont(font);
        accountField1.setBounds(400,260,250,50);
        accountField1.setFont(font);


        loginBtn.setBounds(700,350,140,50);  //转账下标     0 0 宽 高
        loginBtn.addActionListener(this);
        accountLabel.setFont(font);


        this.add(accountLabel);
        this.add(accountLabel1);
        this.add(accountField);
        this.add(accountField1);
        this.add(loginBtn);


        this.setOpaque(false);


    }

    @Override
    public void actionPerformed(ActionEvent e) {
        if (e.getSource()==loginBtn){
             SuperArray user1 =atmFrame.getUsers();
            User user=atmFrame.getLoginUser();


                if (Double.parseDouble(accountField1.getText())>user.getBlance()){
                    JOptionPane.showMessageDialog(this,"余额不足");
                    return;
                }
                if (accountField.getText().equals(user.getAccount())){
                    JOptionPane.showMessageDialog(this,"不能转给自己");
                    return;
                }
            for(int i = 0;i < user1.size();i++){
                User otherUser = (User) user1.get(i);
                if(otherUser.getAccount().equals(accountLabel.getText())){
                    user.setBlance(user.getBlance() - Double.parseDouble(accountField1.getText()));
                    otherUser.setBlance(otherUser.getBlance() + Double.parseDouble(accountField1.getText()));
                    JOptionPane.showMessageDialog(this,"转账成功");
                    atmFrame.switchPanel("menu");
                    return;
                }
            }
            JOptionPane.showMessageDialog(this,"输入的账号不正确");




        }

    }
}
